package com.nana.framework.ui.manager
{
	import com.nana.framework.base.App;
	import com.nana.framework.ui.events.UIEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;

	public class FullScreenPanelManager extends EventDispatcher
	{
		private var modalSprite:Sprite;
		private var panelModalDic:Dictionary;
		private var _stage:Stage;
		private var _parent:DisplayObjectContainer;
		private var panelList:Array;
		
		public function FullScreenPanelManager()
		{
			this._stage = App.appStage;
			this.modalSprite = new Sprite();
			this.modalSprite.graphics.beginFill(0, 0.5);
			this.modalSprite.graphics.drawRect(0, 0, 10, 10);
			this.modalSprite.graphics.endFill();
			this.modalSprite.width = this._stage.stageWidth;
			this.modalSprite.height = this._stage.stageHeight;
			this.panelModalDic = new Dictionary();
			this.panelList = [];
			this._stage.addEventListener(Event.RESIZE, this.resizeHandler);
		}
		
		public function addPanel(panel:DisplayObject, isModal:Boolean=false):void{
			this.panelModalDic[panel] = isModal;
			this.panelList.push(panel);
			if ((this.panelList.length > 0) && (!(this.panelList[(this.panelList.length - 1)] == panel))){
			} else {
				this._addPanel(panel);
			}
			var openEvent:UIEvent = new UIEvent(UIEvent.SCREENPANEL_OPEN);
			openEvent.data = panel;
			dispatchEvent(openEvent);
		}
		
		private function _addPanel(panel:DisplayObject):void{
			panel.visible = true;
			if (this.panelModalDic[panel]){
				this.parent.addChild(this.modalSprite);
			}
			this.addPanelToStage(panel);
		}
		
		protected function addPanelToStage(panel:DisplayObject):void{
			this.parent.addChild(panel);
			panel.x = 0;
			panel.y = 0;
			var popUpEvent:UIEvent = new UIEvent(UIEvent.POPUP_COMPLETE);
			dispatchEvent(popUpEvent);
		}
		
		private function panelComplete():void{
			var popUpEvent:UIEvent = new UIEvent(UIEvent.POPUP_COMPLETE);
			dispatchEvent(popUpEvent);
		}
		
		public function removePanel(panel:DisplayObject):void{
			if (panel && this.parent.contains(panel)){
				this.removePanelToStage(panel);
			}
			if (this.parent.contains(this.modalSprite)){
				this.parent.removeChild(this.modalSprite);
			}
			delete this.panelModalDic[panel];
			var panelIndex:int = this.panelList.indexOf(panel);
			if (panelIndex != -1){
				this.panelList.splice(panelIndex, 1);
			}
			if ((this.panelList.length > 0) && (panelIndex == this.panelList.length)){
				this._addPanel((this.panelList[(this.panelList.length - 1)] as DisplayObject));
			}
			var clsEvent:UIEvent = new UIEvent(UIEvent.SCREENPANEL_CLOSE);
			clsEvent.data = panel;
			dispatchEvent(clsEvent);
		}
		
		protected function removePanelToStage(panel:DisplayObject):void{
			this.parent.removeChild(panel);
		}
		
		public function get parent():DisplayObjectContainer{
			return (this._parent || this._stage);
		}
		
		public function set parent(_arg1:DisplayObjectContainer):void{
			this._parent = _arg1;
		}
		
		public function getPanel():DisplayObject{
			if (this.panelList.length > 0){
				return this.panelList[(this.panelList.length - 1)];
			}
			return null;
		}
		
		public function getPanelList():Array{
			return this.panelList;
		}
		
		private function resizeHandler(event:Event):void{
			this._resizeHandler(this._stage.stageWidth, this._stage.stageHeight);
		}
		
		protected function _resizeHandler(width:Number, height:Number):void{
			var panel:DisplayObject = this.getPanel();
			if (panel != null){
				panel.width = width;
				panel.height = height;
			}
			this.modalSprite.width = width;
			this.modalSprite.height = height;
		}
		
	}
}